<?php
/**
 * 本页面用于单个采集
 * 仅供测试用 
 */
	include_once('../sys/sys.php');
	header('Content-Type: text/html; charset=utf-8');
	$tid = $_GET['tid'];
	
	if(!is_numeric($tid) || $tid < 0){
		show_normal_mesR('TAG ID错误','TAG ID错误','TAG查询');
	}
	
	$url = 'http://www.nonadm.com/manhua/' . $tid . '/';
	
	//分离URL以及ID
	$id = $tid;
	//定义公用变量
	if (substr($url,-1)!="/")
		$url.="/";
	$url_pre = $url;
	$pic_url_pre = 'http://www.nonadm.com/comicdata/';
		
		
	//获取URL内容
//	$url = "1.htm";
	$content = 	file_get_contents($url);
	$content = 	mb_convert_encoding(file_get_contents($url),"UTF-8","GB2312");
	
	//获取名称
	$name = explode('</title>',$content);
	$name = $name[0];
	$name = explode('<title>',$name);
	$name = $name[1];
	$name = explode('--',$name);
	$name = $name[0];
	
	//获取最后更新时间
	$dt = explode('更新时间：<span>',$content);
	$dt = $dt[1];
	$dt = explode('</span>',$dt);
	$dt = strtotime(trim($dt[0]));
	
	//获取URL列表
	$urls = explode('<ul class="bgs" >',$content);
	$urls = $urls[1];
	$urls = explode('</ul>',$urls);
	$urls = $urls[0];
	$replacer = array('target="_blank"','<span>','</span>',' class="tpy"','页');
	$replacerc = array('','','','','');
	$urls = str_replace($replacer,$replacerc,$urls);
	
	$match_str = "/<a href=\"(.*)\" >/isU";
	preg_match_all($match_str,$urls,$urln);
	$urln = $urln[1];
	//获取每单位总数
	$match_str = "/\((.*)\)/isU";
	preg_match_all($match_str,$urls,$numn);
	$numn = $numn[1];
	$ulnum = array($urln,$numn);
	//进入每页进行处理
	
	//插入分类信息
		//检查当前ID是否存在
		$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'COUNT(*) as max','from'   => 'nonadmtags','where'	=> "1 " . $where ,)
		);	
		//当已经存在的时候更新日期
		if ($result['max'] > 0) {
			
		}
		//当不存在时插入
		else{
			do_taginsert($id,$dt,$name,0);
		}
	//获取数据库COUNT
	$where = 'AND id= ' . $id .'';
		$result = $DB->build_and_exec_query(
			array('select' => 'count','from'   => 'nonadmtags','where'	=> "1 " . $where ,)
		);	
	$dbcount = $result['count'];
	
	//由于是逆序，需要倒装数组(链接以及每个链接的数量)
	$urln = array_reverse($urln);
	$numn = array_reverse( $numn );
	
	foreach ($urln as $k=>$v) {
		//当COUNT小于已经采集的时候，跳过。
		if ($k < $dbcount) {
			continue;
		}
		$current_url = $url_pre . $v;
		$comic_contents = mb_convert_encoding(file_get_contents($current_url),"UTF-8","GB2312");
		//获取中间路径
		$dtd = explode('var volpic=\'',$comic_contents);
		$dtd = $dtd[1];
		$dtd = explode('\';var tpf',$dtd);
		$dtd = $dtd[0];
		$complete_url = $pic_url_pre . $dtd;
		//这里添加数据库插入语句
		do_photoinsert($id,$complete_url,$numn[$k]);
	}
	//更新计数
	$where = 'id= ' . $id .'';
	$DB->do_update('nonadmtags',
		array(					  
		   'lastupdate'				=> $dt,
		   'count'					=> count($urln),
		 ),
		 $where
	);
	
		
	
	
	function do_taginsert($id,$lu,$name,$count){
		global $DB;
		$DB->do_insert( 'nonadmtags', array( 'id'			=> $id,
								   'lastupdate'				=> $lu,
								   'name'					=> $name,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	
	function do_photoinsert($uid,$url,$count){
		global $DB;
		$DB->do_insert( 'nonadmphotos', array( 
								   'uid'				=> $uid,
								   'url'					=> $url,
								   'count'					=> $count,
								 )		);
		if ($result === false) {
			show_normal_mesR('无法添加当前漫画','添加出错','添加');
		}
	}
	
	echo "采集完毕";
	
	
	exit;
	
	
?>